﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UDKDevKitVSXDebugEngine
{
    public class VariableInformation
    {
        public static string[] splits = new[] { " ( ", " )", "," };

        public bool m_fUserDefinedType;
        public int m_dwBaseType;
        public int m_dwIndirectionLevel;
        public bool m_fFrameRelative;
        public uint m_address;
        public uint m_parentAddress;

        private string name;

        private List<VariableInformation> children;

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                var parts = value.Split(splits, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length > 1)
                {
                    this.name = parts[0];
                    this.TypeName = parts[1];

                    if (parts.Length > 2)
                    {
                        this.m_address = UInt32.Parse(parts[2], System.Globalization.NumberStyles.HexNumber);
                    }

                    if (parts.Length > 3)
                    {
                        this.m_parentAddress = UInt32.Parse(parts[3], System.Globalization.NumberStyles.HexNumber);
                    }
                }
                else
                {
                    this.name = value.Trim();
                }
            }
        }

        public string FullName 
        { 
            get 
            {
                if (this.Parent == null)
                {
                    return this.Name;
                }
            
                return String.Concat(this.Parent.FullName, '.', this.Name); 
            } 
        }

        public string TypeName { get; set; }

        public string Value { get; set; }

        public bool HasChildren
        {
            get
            {
                return this.children != null && this.children.Count > 0;
            }
        }

        public VariableInformation Parent { get; set; }

        public IList<VariableInformation> Children
        {
            get
            {
                if (this.children == null)
                {
                    this.children = new List<VariableInformation>();
                }

                return this.children;
            }
        }

        public override string ToString()
        {
            return this.Value;
        }

        internal void AddChildren(VariableInformation variable)
        {
            variable.Parent = this;
            this.Children.Add(variable);
        }
    }
}
